import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:mall/core/models/team_and_port.dart';
import 'package:mall/core/services/api.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 开通拼购
class PortOpenSheet extends StatefulWidget {
  const PortOpenSheet({
    Key key,
    @required this.miaId,
    @required this.teamAndPort,
  }) : super(key: key);
  final int miaId;
  final TeamAndPort teamAndPort;

  @override
  _PortOpenSheetState createState() => _PortOpenSheetState();
}

class _PortOpenSheetState extends State<PortOpenSheet> {
  final _formKey = GlobalKey<FormState>();
  String _number;
  String _password;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.fromLTRB(
            8.w,
            8.w,
            8.w,
            8.w + MediaQuery.of(context).viewInsets.bottom,
          ),
          child: Form(
            key: _formKey,
            child: Column(
              children: [
                Text("开通拼购窗口"),
                SizedBox(height: 8.w),
                TextFormField(
                  decoration: InputDecoration(
                    hintText:
                        "开通窗口总数（有${widget.teamAndPort.curPortCount}/共${widget.teamAndPort.maxPortCount}，${widget.teamAndPort.openPortFee}${widget.teamAndPort.openPortAccountTypeName}/窗口）",
                  ),
                  keyboardType: TextInputType.number,
                  onSaved: (value) => _number = value,
                ),
                SizedBox(height: 8.w),
                TextFormField(
                  enabled: widget.teamAndPort.needPassword,
                  decoration: InputDecoration(
                    hintText: widget.teamAndPort.needPassword
                        ? "${widget.teamAndPort.openPortAccountTypeName}密码"
                        : '免密',
                  ),
                  obscureText: true,
                  onSaved: (value) => _password = value,
                ),
                SizedBox(height: 8.w),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    OutlineButton(
                      onPressed: () => Navigator.of(context).pop(),
                      borderSide:
                          BorderSide(color: Theme.of(context).primaryColor),
                      child: Text('放弃支付'),
                    ),
                    RaisedButton(
                      onPressed: _pay,
                      elevation: 0,
                      child: Text('确认支付'),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void _pay() async {
    _formKey.currentState.save();
    final api = context.read<Api>();
    EasyLoading.show();
    try {
      await api.groupPurchaseOpen(
        widget.miaId,
        widget.teamAndPort.openPortAccountType,
        _password,
        int.tryParse(_number),
      );
      EasyLoading.showToast("支付成功");
      Navigator.of(context).pop(true);
    } on DioError catch (e) {
      EasyLoading.showToast("${e.message}");
    }
  }
}
